package cc.eddic.pratice.programmingparadigms.exam.impl;

import cc.eddic.pratice.programmingparadigms.advanced.BeanFactory;
import cc.eddic.pratice.programmingparadigms.exam.ExamService;
import cc.eddic.pratice.programmingparadigms.exam.MasterDataService;
import cc.eddic.pratice.programmingparadigms.exam.QuestionBankService;
import cc.eddic.pratice.programmingparadigms.exam.domain.Answer;
import cc.eddic.pratice.programmingparadigms.exam.domain.Exam;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;
import cc.eddic.pratice.programmingparadigms.exam.domain.Student;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;


public class ExamServiceCxyImpl implements ExamService {
    @Override
    public List<Answer> importAnswers(String answerExcelFile) {
        String content = getFileContent();
        String[] a = content.split("\n");
        MasterDataService masterDataService = BeanFactory.getBean(MasterDataService.class);
        QuestionBankService questionBankService = BeanFactory.getBean(QuestionBankService.class);
        List<Question> questionList = questionBankService.importCorrectAnswers("1");
        List<Answer> answerList = new ArrayList<>();
//        Optional<Exam> examOption = masterDataService.find(masterDataService.getExams(),
//                exam -> "7-12 开营考试".equals(exam.getName()));
        Exam examOption = masterDataService.getExams().stream()
                .filter(option -> option.getName().equals("7-12 开营考试"))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("找不到exam捏"));
        String[] splitByName = a[0].split(",");
        for (int i = 0; i < a.length - 1; i++) {
            String[] c = a[i + 1].split(",", -1);
            for (int k = 0; k < splitByName.length - 1; k++) {
                int finalK = k + 1;
//                Optional<Student> student = masterDataService.find(masterDataService.getStudents(),
//                        exam -> splitByName[finalK].equals(exam.getName()));
                Student student = masterDataService.getStudents().stream()
                        .filter(option -> splitByName[finalK].equals(option.getName()))
                        .findFirst()
                        .orElseThrow(() -> new IllegalArgumentException("找不到该名学生捏"));
                String choice = c[finalK];
                if (choice.equals("")) choice = " ";
                char[] choiceChar = choice.toCharArray();
                int questionNo = Integer.parseInt(c[0]);
//                Optional<Question> questionAns = masterDataService.find(questionList,
//                        p -> questionNo == p.getNo());
                Question questionAns = questionList.stream()
                        .filter(option -> option.getNo() == questionNo)
                        .findFirst()
                        .orElseThrow(() -> new IllegalArgumentException("找不到"));
                answerList.add(new Answer(examOption, questionAns, student, choiceChar[0]));
//                if (examOption.isPresent() && questionAns.isPresent() && student.isPresent())
//                    answerList.add(new Answer(examOption.get(), questionAns.get(), student.get(), choiceChar[0]));
            }
        }
        return answerList;
    }



    private String getFileContent() {
        return "试题编号,李惠,谭雨晴,李馨,曾奕哲,车晓满,王巧家,孙晨玮,崔相元,韩睿洁\n" +
                "1,B,B,B,B,B,B,B,B,C\n" +
                "2,C,C,C,C,C,C,C,C,C\n" +
                "3,A,A,A,A,A,B,A,A,A\n" +
                "4,C,C,C,C,C,C,C,B,C\n" +
                "5,D,D,D,D,D,D,D,D,D\n" +
                "6,B,B,B,B,B,D,B,A,B\n" +
                "7,B,B,B,B,B,C,B,C,B\n" +
                "8,A,A,C,A,A,A,A,A,A\n" +
                "9,D,D,D,D,D,D,A,D,D\n" +
                "10,D,B,D,B,D,D,B,D,D\n" +
                "11,B,A,B,C,A,A,A,A,A\n" +
                "12,C,A,C,A,B,C,C,C,C\n" +
                "13,A,A,B,A,A,A,A,A,B\n" +
                "14,A,A,C,C,D,C,A,C,C\n" +
                "15,A,A,A,A,A,B,A,B,A\n" +
                "16,B,B,B,B,B,B,B,B,B\n" +
                "17,A,C,D,D,D,D,D,D,D\n" +
                "18,C,C,C,C,C,C,C,C,C\n" +
                "19,D,D,D,D,D,D,C,A,D\n" +
                "20,B,B,B,B,B,B,B,B,B\n" +
                "21,D,C,C,A,D,D,,A,\n" +
                "22,D,D,D,,D,C,,C,C\n" +
                "23,A,A,A,A,D,D,,C,C\n" +
                "24,C,C,C,C,C,D,A,C,D\n" +
                "25,B,B,B,B,B,B,,D,B";
    }

    @Getter
     public static class StudentScore {
        public StudentScore(String studentName, int score) {
            this.studentName = studentName;
            this.score = score;
        }

        String studentName;
        int score;
    }

    @Getter
    public static class Model {
        public Model(String studentName, int questionNo, char choice, char correctAnswer) {
            this.studentName = studentName;
            this.questionNo = questionNo;
            this.choice = choice;
            this.correctAnswer = correctAnswer;
        }

        String studentName;
        int questionNo;
        char choice;
        char correctAnswer;
    }


}




